package com.neueedu.android.ticket;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.neueedu.android.R;
import com.neueedu.android.concat.ConcatAddActivity;
import com.neueedu.android.concat.ConcatEditActitiy;
import com.neueedu.android.entity.Ticket;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import androidx.appcompat.app.AppCompatActivity;

public class TicketActivity4 extends AppCompatActivity  {


    String types[] = new String[]{"成人","学生","老人","军人"};
    String names[] = new String[]{"邬昀华",
            "杨音好",
            "万靓侃",
            "章咪海",
            "包菊会",
            "柯宗福",
            "叶深宝",
            "萧嵘纳",
            "薄朴军",
            "裴俭杨",
            "申京姿",
            "裴曦尤",
            "郎建盟",
            "钱洁惟",
            "蒙向锬",
            "祝沛闯",
            "裴影霏",
            "鲍雪玥",
            "贺骊昭",
            "尹笑漫",
            "黎海荷",
            "仰岭冰",
            "晏剑伊",
            "吴河溢",
            "薛艺沁",
            "虞拓屹",
            "奚涓琴",
            "怀姗琦",
            "丁宝焰",
            "庞江歆",
            "顾耘畏"};

    Random rand = new Random();
    List<HashMap<String, Object>> data = new ArrayList<>();
    private List<Boolean> selectConcat  = null;

    //ListView的数据

    SelectConcatAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_concat);


        //1 找到  ListView
        ListView lvConcatList = findViewById(R.id.lvConcatList);
        Button btnAddConcat = findViewById(R.id.btnAddConcat);


        //2 创建一个适配器  并绑定数据

        selectConcat = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            HashMap<String,Object>  row = new HashMap<String,Object>();

            row.put("index",i+1);
            row.put("selected",false);
            row.put("name",names[i]);
            row.put("idno", UUID.randomUUID().toString());
            row.put("type", types[rand.nextInt(types.length-1)]);

            data.add(row);
            selectConcat.add(false);
        }



        adapter = new SelectConcatAdapter(data );


        //3 将适配器和 ListView绑定
        lvConcatList.setAdapter(adapter);

        btnAddConcat.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                //选中的联系人
                ArrayList<Map> selectConcatList = new ArrayList<>();

                String names = "";
                for (int i = 0; i < data.size(); i++) {
                    HashMap<String, Object> row = data.get(i);
                    boolean selected = (boolean) row.get("selected");
                    if(selected){

                        selectConcatList.add(row);

                        names +=  row.get("name")+"  ";
                    }
                }

                Intent intent = new Intent();
                intent.putExtra("selectConcatList",selectConcatList);
                setResult(100,intent);

                finish();

                Toast.makeText(TicketActivity4.this, names, Toast.LENGTH_LONG).show();




            }
        });

    }




    class SelectConcatAdapter extends BaseAdapter{

        List<HashMap<String, Object>> concatList ;


        public SelectConcatAdapter(List<HashMap<String, Object>> concatList) {
            this.concatList = concatList;
        }

        @Override
        public int getCount() {
            return concatList.size();
        }

        @Override
        public Object getItem(int position) {
            return concatList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v;
            ViewHolder viewHolder;
            final Map data = (Map) getItem(position);
            if(convertView == null){
                v = getLayoutInflater().inflate(R.layout.activity_ticket_list_item,null);
                viewHolder = new ViewHolder();
                viewHolder.ckSelectConcat = v.findViewById(R.id.ckSelectConcat);
                viewHolder.tvName = v.findViewById(R.id.tvName);
                viewHolder.tvIdNo = v.findViewById(R.id.tvIdNo);
                viewHolder.tvType = v.findViewById(R.id.tvType);

                v.setTag(viewHolder);
            }else{
                v=  convertView;
                viewHolder = (ViewHolder) v.getTag();
            }

            //viewHolder
            viewHolder.ckSelectConcat.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    boolean selected = (boolean) data.get("selected");
                    data.put("selected",!selected);
                }
            });

            viewHolder.tvName.setText((String) data.get("name"));
            viewHolder.tvIdNo.setText((String) data.get("idno"));
            viewHolder.tvType.setText((String) data.get("type"));

            return v;
        }

        class ViewHolder {
            CheckBox ckSelectConcat;
            TextView tvName;
            TextView tvIdNo;
            TextView tvType;

        }
    }



}
